package com.xkcyy.one.dto;

import com.xkcyy.one.common.EntityDTO;
import com.xkcyy.one.entity.Conversation;
import com.xkcyy.one.entity.Message;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对话数据传输对象
 *
 * @author AI Assistant
 */
@Data
public class ConversationDTO extends EntityDTO {

    /**
     * 对话标题
     */
    private String title;
    
    /**
     * 用户ID
     */
    private Long userId;
    

    /**
     * 消息列表
     */
    private List<MessageDTO> messages;

    /**
     * 将实体转换为DTO
     *
     * @param conversation 对话实体
     * @return 对话DTO
     */
    public static ConversationDTO fromEntity(Conversation conversation) {
        if (conversation == null) {
            return null;
        }
        ConversationDTO conversationDTO = new ConversationDTO();
        BeanUtils.copyProperties(conversation, conversationDTO);
        
        if (conversation.getMessages() != null) {
            List<MessageDTO> messageDTOs = conversation.getMessages().stream()
                    .map(MessageDTO::fromEntity)
                    .collect(Collectors.toList());
            conversationDTO.setMessages(messageDTOs);
        }
        
        return conversationDTO;
    }

    /**
     * 将DTO转换为实体
     *
     * @return 对话实体
     */
    public Conversation toEntity() {
        Conversation conversation = new Conversation();
        BeanUtils.copyProperties(this, conversation);
        
        if (this.getMessages() != null) {
            List<Message> messages = this.getMessages().stream()
                    .map(MessageDTO::toEntity)
                    .collect(Collectors.toList());
            conversation.setMessages(messages);
        }
        
        return conversation;
    }
}
